﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 坦克大战.Properties;

namespace 坦克大战
{
    internal class MyTank:Movingthing
    {
        public int HP {  get; set; }
        public bool IsMoving { get; set; }
        private int orginalX;
        private int orginalY;
        public MyTank(int x,int y,int speed) 
        {
            this.X = x;
            this.Y = y;
            orginalX=x; orginalY=y;
            this.Speed = speed;
            this.BitmapLeft = Resources.MyTankLeft;
            this.BitmapUp = Resources.MyTankUp;
            this.BitmapRight = Resources.MyTankRight;
            this.BitmapDown = Resources.MyTankDown;
            this.Dir = Dirction.Up;
            HP = 4;
        
        
        }

        public override void Update()
        {
            MoveCheck();//移动检查
            Move();

            base.Update();
        }

        private void MoveCheck()
        {

            #region 检查有没有超过窗体边界
            if (Dir == Dirction.Up)
            {
                if (Y - Speed < 0)
                {
                    IsMoving = false; return;
                }
            }
            else if (Dir == Dirction.Down)
            {
                if (Y + Speed + Height > 450)
                {
                    IsMoving = false; return;
                }
            }
            else if (Dir == Dirction.Left)
            {
                if (X - Speed < 0)
                {
                    IsMoving = false; return;
                }
            }
            else if (Dir == Dirction.Right)
            {
                if (X + Speed + Width > 450)
                {
                    IsMoving = false; return;
                }
            }
            #endregion


            //检查有没有和其他元素发生碰撞

            Rectangle rect = GetRectangle();

            switch (Dir)
            {
                case Dirction.Up:
                    rect.Y -= Speed;
                    break;
                case Dirction.Down:
                    rect.Y += Speed;
                    break;
                case Dirction.Left:
                    rect.X -= Speed;
                    break;
                case Dirction.Right:
                    rect.X += Speed;
                    break;
            }

            if (GameObjectManager.IsCollidedWall(rect) != null)
            {
                IsMoving = false; return;
            }
            if (GameObjectManager.IsCollidedSteel(rect) != null)
            {
                IsMoving = false; return;
            }
            if (GameObjectManager.IsCollidedBoss(rect))
            {
                IsMoving = false; return;
            }
        }
        public void TakeDamage()
        {
            HP--;
            if(HP<=0)
            {
                X = orginalX;
                Y = orginalY;
                HP = 4;
            }
        }
        private void Move()
        {
            if (IsMoving == false) return;

            switch (Dir)
            {
                case Dirction.Up:
                    Y -= Speed;
                    break;
                case Dirction.Down:
                    Y += Speed;
                    break;
                case Dirction.Left:
                    X -= Speed;
                    break;
                case Dirction.Right:
                    X += Speed;
                    break;
            }
        }

        public void KeyDown(KeyEventArgs e)
        {


            switch (e.KeyCode)
            {
                case Keys.W:
                    Dir = Dirction.Up;
                    IsMoving = true;
                    break;
                case Keys.S:
                    Dir = Dirction.Down;
                    IsMoving = true;
                    break;
                case Keys.A:
                    Dir = Dirction.Left;
                    IsMoving = true;
                    break;
                case Keys.D:
                    Dir = Dirction.Right;
                    IsMoving = true;
                    break;
                case Keys.Space:
                    Attack();
                    break;

            }

        }


        private void Attack()
        {
            SoundManager.PlayFir();
            int x = this.X;
            int y = this.Y;

            switch (Dir)
            {
                case Dirction.Up:
                    x += Width/2;
                    break;
                case Dirction.Down:
                    x += Width / 2;
                    y += Height;
                    break;
                case Dirction.Left:
                    y += Height / 2;
                    break;
                case Dirction.Right:
                    x += Width;
                    y += Height / 2;
                    break;
            
            
            }
            


            GameObjectManager.CreateBullet(x,y,Tag.MyTank, Dir);
        }

        public void KeyUp(KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.W:

                    IsMoving = false;
                    break;
                case Keys.S:

                    IsMoving = false;
                    break;
                case Keys.A:

                    IsMoving = false;
                    break;
                case Keys.D:

                    IsMoving = false;
                    break;

            }
        }

    }
}
